#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        vector<int> hash(1001);
        for (auto a : arr1)
        {
            hash[a]++;
        }

        int count = 0;
        for (int i = 0; i < arr2.size(); i++)
        {
            while (hash[arr2[i]] > 0)
            {
                arr1[count++] = arr2[i];
                hash[arr2[i]]--;
            }
        }

        for (int i = 0; i < hash.size(); i++)
        {
            while (hash[i] > 0)
            {
                arr1[count++] = i;
                hash[i]--;
            }
        }

        return arr1;
    }
};